<template>
	<div>
		<Card>
			监理列表
			<Icon type="ios-arrow-forward" /> 监理师列表
		</Card>
		<!-- 头部搜索区域 -->
		<Card>
		<div class="header-search">
			<div>
				<Input class="input" v-model="nameSearch" placeholder="监理师姓名" clearable />
				<Button type="primary" icon="ios-search" @click="search">搜索</Button>
			</div>
				<span @click="addSupervision" style="cursor: pointer;"><Icon type="md-add" style="font-size: 20px;margin-top: -3px;"/>新增监理师</span>
		</div>
		<br /><br />
		<!-- 用户信息列表 -->
		<Table border :columns="columns1" :data="userList"></Table>
		<!-- 分页 -->
		<br /><br />
		<div style="text-align: center;">
			<Page :total="total" show-elevator @on-change="changeUser" />
		</div>
		</Card>
	</div>
</template>
<script>
	import path from '@/api/path.js';
	let url = path.url;
	import axios from '@/libs/api.request';
	export default {
		name: 'level_1',
		data() {
			return {
				nameSearch: "",
				columns1: [
					{
						title: '编号',
						key: 'id',
						width: 100,
						align: 'center'
					},
					{
						title: '监理师姓名',
						key: 'name',
						width: 100,
						align: 'center'
					},
					{
						title: '监理师头像',
						key: 'userPicture',
						width: 100,
						align: 'center',
						columns: {
							'width': '50px'
						},
						render: (h, params) => {
							return h('div', [
								h('img', {
									attrs: {
										src: params.row.userPicture
									},
									style: {
										width: '40px',
										height: '40px'
									}
								}),
							]);
						}
					},
					{
						title: '手机号',
						key: 'mobile',
						width: 200,
						align: 'center'
					},
					{
						title: '介绍',
						key: 'userIntroduce',
						align: 'center'
					},
					{
						title: '操作',
						key: 'action',
						width: 250,
						align: 'center',
						render: (h, params) => {
							return h('div', [
								h('Button', {
									props: {
										type: 'primary',
										size: 'small'
									},
									style: {
										marginRight: '10px'
									},
									on: {
										click: (e) => {
											this.goDetail(params.row);
										}
									}
								}, '查看详情'),
								h('Button', {
									props: {
										type: 'error',
										size: 'small'
									},
									on: {
										click: () => {
											this.delect(params.row)
										}
									}
								}, '删除')
							]);
						}
					}
				],
				userList: [],
				total: 0,
				page: 1
			}
		},
		methods: {
			// 搜索
			search() {
				this.getUserInfo();
			},
			// 获取列表
			getUserInfo() {
				axios.request({
					url: `${url}supervision/getSupervisionList`,
					method: 'post',
					data: {
						page: this.page,
						name: this.nameSearch
					}
				}).then(res => {
					if (res.data.status == 1) {
						this.userList = res.data.content.data;
						this.total = res.data.content.total;
					} else {
						throw '获取监理师列表失败！'
					}
				})
			},
			// 进入详情
			goDetail(obj) {
				if (obj) {
					this.$router.push({
						name: 'siteList',
						query: {
							id: obj.id
						}
					});
				} else {
					this.$router.push({
						name: '/'
					});
				}
			},
			// 分页
			changeUser(idx) {
				this.page = idx;
				this.getUserInfo();
			},
			// 删除
			delect(obj) {
				axios.request({
					url: `${url}supervision/delSupervision`,
					method: 'post',
					data: {
						id: obj.id
					}
				}).then(res=>{
					if ( res.data.status == 1 ) {
						this.getUserInfo();
					} else {
						throw '删除失败！'
					}
				})
			},
			// 新增监理师
			addSupervision(){
				this.$router.push('supervisor/addSupervisor');
			}
		},
		created() {
			this.getUserInfo();
		}
	}
</script>
<style scoped="scoped">
	.header-search {
		display: flex;
		justify-content: space-between;
	}

	.input {
		width: 200px;
		margin-right: 20px;
	}
</style>
